package structs

import (
	"encoding/json"
	"fmt"
	"github.com/shopspring/decimal"
	"ir/models/tables"
)

type GoodsOrder struct {
	OrderSn   string `json:"order_sn"`
	Symbol    string `json:"symbol"`
	AccountId string `json:"account_id"`
	Amount    string `json:"amount"`
	Price     string `json:"price"`
	Side      int    `json:"side"`
}

type SymbolOrder struct {
	ID      string  `json:"id"`
	UserId  string  `json:"user_id"`
	Amount  float64 `json:"amount"`
	Price   float64 `json:"price"`
	Side    int     `json:"side"`
	Created int64   `json:"created"`
}

func (order *SymbolOrder) FromJSON(msg []byte) error {
	return json.Unmarshal(msg, order)
}

func (order *SymbolOrder) ToJSON() []byte {
	str, _ := json.Marshal(order)
	return str
}

type Order struct {
	SymbolOrder
	Type   string
	Index  int64
	Symbol string
	Ip     string
}

func (order *Order) FromJSON(msg []byte) error {
	return json.Unmarshal(msg, order)
}

func (order *Order) ToJSON() []byte {
	str, _ := json.Marshal(order)
	return str
}

func (order *Order) ToOrderBook() tables.OrderBook {
	dPrice := decimal.NewFromFloat(order.Price)
	dAmount := decimal.NewFromFloat(order.Amount)
	amount := dAmount.String()
	return tables.OrderBook{
		Sn:      order.ID,
		UserId:  order.UserId,
		Price:   dPrice.String(),
		Amount:  amount,
		Total:   amount,
		Symbol:  order.Symbol,
		Side:    order.Side,
		Created: order.Created,
		Ip:      order.Ip,
	}
}

func (order *Order) Key() string {
	return fmt.Sprintf("lo-%d-%s", order.Side, order.Symbol)
}
